package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjProductParamFieldVal;
import com.zb.entity.ZjProductParamFieldValOption;
import com.zb.extend.ProductParamFieldValExtend;
import com.zb.mapper.ZjProductParamFieldValMapper;
import com.zb.service.ZjProductParamFieldValOptionService;
import com.zb.service.ZjProductParamFieldValService;
import com.zb.vo.GoodsFieldValVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品参数字段值关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-18
 */
@Service
public class ZjProductParamFieldValServiceImpl extends ServiceImpl<ZjProductParamFieldValMapper, ZjProductParamFieldVal> implements ZjProductParamFieldValService {
    @Resource
    private ZjProductParamFieldValOptionService productParamFieldValOptionService;

    // 新增或修改商品参数字段
    @Override
    @Transactional
    public void save(Long custAccId, Long productParamId, List<GoodsFieldValVo> fieldValVos) {
        if (null == fieldValVos || fieldValVos.isEmpty()) {
            return;
        }
        productParamFieldValOptionService.deleteByByProductParamId(productParamId);
        List<ZjProductParamFieldVal> adds = new ArrayList<>();
        List<ZjProductParamFieldVal> updates = new ArrayList<>();
        ZjProductParamFieldVal fieldVal = null;
        List<ZjProductParamFieldValOption> addOptVals = new ArrayList<>();
        ZjProductParamFieldValOption  option = null;
        for (GoodsFieldValVo vo : fieldValVos) {
            fieldVal = new ZjProductParamFieldVal();
            BeanUtils.copyProperties(vo, fieldVal);
            if (null != fieldVal.getId()) {
                updates.add(fieldVal);
            } else {
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setProductParamId(productParamId);
                adds.add(fieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                option = new ZjProductParamFieldValOption();
                BeanUtil.copyProperties(optVo, option);
                option.setId(IdWorker.getId());
                option.setCustAccId(custAccId);
                option.setProductParamId(productParamId);
                option.setProductParamFieldValId(fieldVal.getId());
                addOptVals.add(option);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        if (CollectionUtil.isNotEmpty(addOptVals)) productParamFieldValOptionService.saveBatch(addOptVals);
    }

    // 根据产品参数ID获取产品参数字段
    @Override
    public List<ProductParamFieldValExtend> getByProductParamId(Long productParamId) {
        QueryWrapper<ZjProductParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "goods_param_field_id", "type", "name");
        wrapper.eq("product_param_id", productParamId);
        List<ZjProductParamFieldVal> list = this.list(wrapper);
        List<ZjProductParamFieldValOption> options = productParamFieldValOptionService.getByProductParamFieldValIds(list.stream().map(ZjProductParamFieldVal::getId).toList());
        return list.stream().map(fielVal -> {
            ProductParamFieldValExtend extend = new ProductParamFieldValExtend();
            BeanUtil.copyProperties(fielVal, extend);
            extend.setOptVos(options.stream().filter(f -> Objects.equals(f.getProductParamFieldValId(), fielVal.getId())).map(option -> {
                return new FieldValOptionVo(option.getId(), option.getOptValue());
            }).toList());
            return extend;
        }).toList();
    }

    // 根据产品参数ID删除信息
    @Override
    public void deleteByProductParamIds(List<Long> productParamIds) {
        if (CollectionUtils.isEmpty(productParamIds)) {
            return;
        }
        productParamFieldValOptionService.deleteByByProductParamIds(productParamIds);
        QueryWrapper<ZjProductParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.in("product_param_id", productParamIds);
        this.remove(wrapper);
    }

    // 根据商品参数字段ID查询
    @Override
    public List<Long> getByGoodsParamFieldIds(List<Long> goodsParamFieldId) {
        if (CollectionUtils.isEmpty(goodsParamFieldId)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjProductParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("goods_param_field_id");
        wrapper.in("goods_param_field_id", goodsParamFieldId);
        return this.list(wrapper).stream().map(ZjProductParamFieldVal::getGoodsParamFieldId).distinct().toList();
    }
}
